package net.csdn.business.mirror.scheduled;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.utils.JsonUtil;
import net.csdn.business.mirror.config.ImportMirrorConfig;
import net.csdn.business.mirror.config.ThirdPartInfoMirrorConfig;
import net.csdn.business.mirror.entity.bean.ThirdPartMirrorInfo;
import net.csdn.business.mirror.mapper.ThirdPartMirrorInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ImportThirdPartMirrorInfoScheduled {

    @Autowired
    private ThirdPartMirrorInfoMapper thirdPartMirrorInfoMapper;

    @Autowired
    private ThirdPartInfoMirrorConfig thirdPartInfoMirrorConfig;
    @Scheduled(cron = "0 05 02 * * ?")
    public void executeTask() {
        String accessToken = thirdPartInfoMirrorConfig.getGiteeAccesstoken();
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "token " + accessToken);
        String url = "";
        for (int i = 74;i < 300;i++){
            url = "https://gitee.com/api/v5/orgs/mirrors/repos?type=all&page=" + i + "&per_page=100";
            HttpEntity<String> entity = new HttpEntity<>(headers);
            String body = restTemplate.exchange(url, HttpMethod.GET, entity,String.class).getBody();
            List<ThirdPartMirrorInfoForGitee> thirdPartMirrorInfoForGitees = JSONObject.parseArray(body, ThirdPartMirrorInfoForGitee.class);
            for (int j = 0; j < thirdPartMirrorInfoForGitees.size(); j++) {
                ThirdPartMirrorInfoForGitee thirdPartMirrorInfoForGitee =  thirdPartMirrorInfoForGitees.get(j);
                ThirdPartMirrorInfo thirdPartMirrorInfo = new ThirdPartMirrorInfo();
                // 查询数据库中是否存在该条数据
                Long countByNameAndSource = thirdPartMirrorInfoMapper.countByNameAndSource(thirdPartMirrorInfoForGitee.getName(), "Gitee");
                if (countByNameAndSource != 0){
                    continue;
                }
                thirdPartMirrorInfo.setName(thirdPartMirrorInfoForGitee.getName());
                thirdPartMirrorInfo.setPathWithNamespace(thirdPartMirrorInfoForGitee.getHtml_url());
                thirdPartMirrorInfo.setCreatedAt(thirdPartMirrorInfoForGitee.getCreated_at());
                thirdPartMirrorInfo.setUpdatedAt(thirdPartMirrorInfoForGitee.getUpdated_at());
                thirdPartMirrorInfo.setSshUrl(thirdPartMirrorInfoForGitee.getSsh_url());
                thirdPartMirrorInfo.setStargazersCount((long) thirdPartMirrorInfoForGitee.getStargazers_count());
                thirdPartMirrorInfo.setWatchersCount((long) thirdPartMirrorInfoForGitee.getWatchers_count());
                if (thirdPartMirrorInfoForGitee.getLanguage() != null){
                    thirdPartMirrorInfo.setLanguage(thirdPartMirrorInfoForGitee.getLanguage().toString());
                }
                thirdPartMirrorInfo.setForksCount((long) thirdPartMirrorInfoForGitee.getForks_count());
                thirdPartMirrorInfo.setOpenIssuesCount((long) thirdPartMirrorInfoForGitee.getOpen_issues_count());
                if (thirdPartMirrorInfoForGitee.getLicense() != null){
                    thirdPartMirrorInfo.setLicense(thirdPartMirrorInfoForGitee.getLicense().toString());
                }
                thirdPartMirrorInfo.setSource("Gitee");
                thirdPartMirrorInfo.setPlatform("");
                thirdPartMirrorInfoMapper.insert(thirdPartMirrorInfo);
            }
        }
    }

}
